﻿@model Coevery.Fields.Settings.CurrencyFieldSettings
@using Coevery.Fields.Settings;

<section class="control-group data-row">
    <label class="control-label">
        Length
    </label>
    <div class="controls errortips">
        @Html.TextBoxFor(l => l.Length, new { @class = "span12 digits", id = "currencyLength", name = "currencyLength" })
    </div>
</section>

<section class="control-group data-row">
    <label class="control-label">
        Decimal Places
    </label>
    <div class="controls errortips">
        @Html.TextBoxFor(d => d.DecimalPlaces, new { @class = "span12 digits", id = "decimalPlace", name = "decimalPlace" })
    </div>
</section>

<section class="control-group data-row">
    <label class="control-label">
        Default Value
    </label>
    <div class="controls errortips">
        @Html.TextBoxFor(d => d.DefaultValue, new { @class = "span12", id = "currencyDefault", name = "currencyDefault" })
    </div>
</section>

<script type="text/javascript">
    $(function () {
        var intLen = $("#currencyLength").val();
        var decLen = $("#decimalPlace").val();
        $("#currencyDefault").attr("decimalFormat", [intLen, decLen]);
        
        $("#currencyLength").keyup(function () {
            var tempValue = $("#currencyLength").val();
            if ($.isNumeric(tempValue) && Math.floor(tempValue) == tempValue) {
                intLen = tempValue;
            } else {
                intLen = 0;
            }
            $("#currencyDefault").attr("decimalFormat", [intLen, decLen]);
        });
        $("#decimalPlace").keyup(function () {
            var tempValue = $("#decimalPlace").val();
            if ($.isNumeric(tempValue) && Math.floor(tempValue) == tempValue) {
                decLen = tempValue;
            } else {
                decLen = 0;
            }
            $("#currencyDefault").attr("decimalFormat", [intLen, decLen]);
        });

        $.validator.addMethod("decimalFormat", function (value, element, params) {
            if (!value) {
                return true;
            }
            var paramArray = params.split(',');
            var tempInt = parseInt(value);
            return !isNaN(tempInt) && tempInt.toString().length <= paramArray[0] &&
                RegExp("^(\\d{0," + paramArray[0] + "})(\.\\d{0," + paramArray[1] + "})?$").test(value);
        }, "Wrong decimal format!");
        $.validator.classRuleSettings.decimalFormat = { decimalFormat: true };
    });
</script>
